package com.yenlien.traceyun.breed.controller;

import com.yenlien.traceyun.breed.dao.BreedScreenDao;
import com.yenlien.traceyun.breed.entity.BreedScreen;
import com.yenlien.traceyun.breed.feign.ProviderClient;
import com.yenlien.traceyun.business.interceptor.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 养殖大屏部分数据
 * @author ChenXi
 * @date 2023/7/4
 */
@RestController
@RequestMapping("/screen")
public class BreedBigScreenController {

    @Autowired
    private BreedScreenDao screenDao;


    @Autowired
    private PlatformTransactionManager transactionManager;
    @Autowired
    private ProviderClient providerClient;

    @PassToken
    @GetMapping("/getData")
    public Object getData() {
        List<BreedScreen> breedScreenDaos = screenDao.findAll();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> mapSum = new HashMap<>();
        //入驻企业数据
        List<BreedScreen> list = breedScreenDaos.stream().filter(x -> x.getFirmName() != null).collect(Collectors.toList());
        //牦牛生产数据
        Set<Map<String, Object>> maps = breedScreenDaos.stream().filter(x -> x.getQxcl() != null && x.getDfms() != null)
                .map(x -> {
                    map.put("qxcl", x.getQxcl());
                    map.put("jtmc", providerClient.findAllPlot());
                    map.put("cnn", x.getAdultCow());
                    map.put("ynn", x.getInfancyCow());
                    map.put("yfms", x.getYfms());
                    map.put("dfms", x.getDfms());
                    return map;
                }).collect(Collectors.toSet());
        mapSum.put("rzqysj", list); //入驻企业数据
        mapSum.put("mnscsj", maps);  //牦牛生产数据
        return mapSum;
    }

    @Scheduled(cron = "0 0 0/24 * * ?")
    public void addData() {
        // 开启一个新的事务
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        //查询全部信息
        List<BreedScreen> breedScreenDaos = screenDao.findAll();
        //入驻企业数据
        List<BreedScreen> list = breedScreenDaos.stream().map(d -> {
            if (d.getFirmName() != null) {
                d.setYaknum(d.getYaknum() + 5);
            } else {
                d.setQxcl(d.getQxcl() + 600);
                d.setAdultCow(d.getAdultCow() + 450);
                d.setInfancyCow(d.getInfancyCow() + 150);
                d.setYfms(d.getYfms() + 600);
                d.setDfms(d.getDfms() + 600);
            }
            return d;
        }).collect(Collectors.toList());
        try {
        screenDao.saveAll(list);
        } catch (Exception e) {
            //手动回滚
            transactionManager.rollback(status);
            throw new RuntimeException(e);
        } finally {
            //提交事务
            transactionManager.commit(status);
        }
    }
}
